//
//  MovieAllViewController.m
//  product_B
//
//  Created by lanou on 16/6/1.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "MovieAllViewController.h"
#import "MovieStoryModel.h"
#import "MovieStoryTableViewCell.h"

@interface MovieAllViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSString *lastId;

@end

@implementation MovieAllViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.dataArray = [NSMutableArray array];
   
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"电影故事";
    
    [self netRequest];
    
    self.tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, kWidth, kHeight - 64) style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    [self.view addSubview:_tableView];
    
    [self.tableView registerNib:[UINib nibWithNibName:@"MovieStoryTableViewCell" bundle:nil] forCellReuseIdentifier:@"storyTableViewCell"];
    self.tableView.rowHeight = UITableViewAutomaticDimension;
    self.tableView.estimatedRowHeight = 10;
    self.tableView.allowsSelection = NO;
    
    self.lastId = 0;
    self.tableView.mj_footer = [MJRefreshAutoFooter footerWithRefreshingBlock:^{
        if (self.dataArray.count == 0) {
            self.lastId = @"0";
        }else{
             MovieStoryModel *model = self.dataArray[self.dataArray.count-1];
        self.lastId = model.myId;
        }
       
        [self netRequest];
    }];
    
}

- (void)netRequest{
    AFHTTPSessionManager *manager = [AFHTTPSessionManager manager];
    manager.responseSerializer = [AFHTTPResponseSerializer serializer];
    [manager GET:[NSString stringWithFormat:@"http://v3.wufazhuce.com:8000/api/movie/%@/story/0/%@",self.movie_id,self.lastId] parameters:nil progress:^(NSProgress * _Nonnull downloadProgress) {
        
    } success:^(NSURLSessionDataTask * _Nonnull task, id  _Nullable responseObject) {
        NSDictionary *dataDic = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers error:nil];
        NSDictionary *datadic = dataDic[@"data"];
        NSArray *dataArray = datadic[@"data"];
        for (NSDictionary *dic in dataArray) {
            MovieStoryModel *model = [[MovieStoryModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.dataArray addObject:model];
        }
        dispatch_async(dispatch_get_main_queue(), ^{
            [self.tableView reloadData];
            [self performSelector:@selector(endendend) withObject:self afterDelay:1];
            
        });
    } failure:^(NSURLSessionDataTask * _Nullable task, NSError * _Nonnull error) {
        
    }];
}
- (void)endendend{
    [self.tableView.mj_footer endRefreshing];
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    MovieStoryTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"storyTableViewCell" forIndexPath:indexPath];
    MovieStoryModel *model = self.dataArray[indexPath.row];
    NSString *username = [model.user objectForKey:@"user_name"];
    NSString *weburl = [model.user objectForKey:@"web_url"];
    [cell.picImageView sd_setImageWithURL:[NSURL URLWithString:weburl]];
    cell.picImageView.layer.cornerRadius = 55/2;
    cell.picImageView.layer.masksToBounds = YES;
    cell.authorLabel.text = username;
    cell.dateLabel.text = [model.input_date substringToIndex:10];
    cell.titleLabel.text = model.title;
    cell.countLabel.text = [NSString stringWithFormat:@"%@",model.praisenum];
    cell.contentLabel.text = model.content;
    cell.scoreLabel.text = @"";
    return cell;

}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    self.tabBarController.tabBar.hidden = YES;
}
- (void)searchButtonAction:(UIButton *)sender{
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
